import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter)

import store from "@/store"
import Layout from "@/views/layout.vue"
import Home from "@/views/home.vue"
import About from "@/views/about.vue"
import Product from "@/views/product.vue"
import ThundersTech from "@/views/thundersTech.vue"
import Application from "@/views/application.vue"
import News from "@/views/news.vue"
import NewsDetail from "@/views/newsDetail.vue"

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "home",
        component: Home,
        name: "home",
        meta: {
          title: "Thunders Biotech",
          description:
            "Discover Thunders Biotech, pioneers in miniaturized functional ultrasound (fUS) systems. Our innovative mini-fUS technology unlocks new frontiers in brain research.",
        },
      },
      {
        path: "about",
        component: About,
        name: "about",
        meta: {
          title: "Thunders Biotech - About",
          description:
            "Learn about Thunders Biotech's mission to democratize functional ultrasound. Meet our expert leadership team dedicated to advancing neuroscience and BCI technology",
        },
      },
      {
        path: "product",
        component: Product,
        name: "product",
        meta: {
          title: "Thunders Biotech - Product",
          description:
            "Explore Thunders Biotech's product suite, featuring the revolutionary mini-fUS system, high-performance probes, and intuitive software for advanced neuroscience research",
        },
      },
      {
        path: "thundersTech",
        component: ThundersTech,
        name: "thundersTech",
        meta: {
          title: "Thunders Biotech - ThundersTech",
          description:
            "Delve into ThundersTech, the core technology driving Thunders Biotech's mini-fUS systems. Learn about our innovations in miniaturization, plane-wave imaging & GPU processing",
        },
      },
      {
        path: "application",
        component: Application,
        name: "application",
        meta: {
          title: "Thunders Biotech - Applications",
          description:
            "Explore diverse neuroscience applications of Thunders Biotech's mini-fUS system, including rsFC, functional neuroimaging, vascular imaging, and neuropharmacology",
        },
      },
      {
        path: "news",
        component: News,
        name: "news",
        meta: {
          title: "Thunders Biotech - News",
          description:
            "Stay informed with the latest news, research breakthroughs, and event announcements from Thunders Biotech, a leader in functional ultrasound technology",
        },
      },
      {
        path: "news/:id",
        component: NewsDetail,
        name: "newsDetail",
        meta: {
          title: "Thunders Biotech - News",
          description:
            "Stay informed with the latest news, research breakthroughs, and event announcements from Thunders Biotech, a leader in functional ultrasound technology",
        },
      },
    ],
  },
]

// 获取原型对象push函数
const originalPush = VueRouter.prototype.push
// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

const router = new VueRouter({
  mode: "history", // 使用 HTML5 History 模式
  base: "/",
  routes,
})

router.afterEach((to) => {
  document.querySelector('meta[name="description"]').setAttribute("content", to.meta.description)
  document.title = to.meta.title || "默认标题" // 设置页面标题
  window.scrollTo(0, 0)
  store.commit("setCurrentRoute", to.path)
})

export default router
